<?php
// +----------------------------------------------------------------------
// | INPHP
// | Copyright (c) 2023 https://inphp.cc All rights reserved.
// | Author: 幺月儿(https://gitee.com/lulanyin) Email: inphp@qq.com
// | 该文件的开源协议以所在项目的LICENSE文件为准，请遵守开源协议要求
// +----------------------------------------------------------------------
// | 统计标签的文章数量
// +----------------------------------------------------------------------
namespace app\cms\cmd;

use app\cms\model\ArchiveModel;
use app\cms\model\TagsModel;
use Inphp\Core\Config;
use Inphp\Core\Util\Log;

class countTagsArchives
{
    //指定ID
    public array $ids = [];

    //指定进程，如果是0，则是主进程
    public int $pid = 0;

    //处理的ID区间
    public int $startId = -1;

    //每条进程工作量
    public int $mission = 500;

    public function __construct(array $params = [])
    {
        $id = $params["id"] ?? "";
        $ids = !empty($id) ? explode(",", $id) : [];
        foreach ($ids as $id) {
            if (is_numeric($id) && $id > 0 && !in_array(ceil($id), $this->ids)) {
                $this->ids[] = ceil($id);
            }
        }
        //指定PID
        $pid = $params["pid"] ?? 0;
        $this->pid = is_numeric($pid) ? ceil($pid) : 0;
        //ID区间
        $start = $params["start"] ?? -1;
        $this->startId = is_numeric($start) && $start >= 0 ? ceil($start) : -1;
        //工作量
        $mission = $params["mission"] ?? 500;
        $this->mission = is_numeric($mission) && $mission >= 100 ? ceil($mission) : 500;
        $this->mission = min(1000, $this->mission);
    }

    public function run()
    {
        if ($this->pid === 0 && empty($this->ids)) {
            //主进程，通过获取最大ID，然后每条进程分发500条数据，同时处理
            $maxId = TagsModel::emptyQuery()->max("id");
            if ($maxId > 0) {
                $this->debug("取到最大ID值：{$maxId}");
                //计算需要进程数量
                $processLen = ceil($maxId / $this->mission);
                for ($i = 1; $i <= $processLen; $i++) {
                    $start = ($i - 1) * $this->mission;
                    $this->debug("已分发进程：{$i}");
                    CMD_RUN("app.cms.cmd.countTagsArchives", [
                        "pid"   => $i,
                        "start" => $start
                    ], true);
                }
            }
            return;
        }
        $db = TagsModel::emptyQuery();
        if (!empty($this->ids)) {
            $db->whereIn("id", $this->ids);
        }
        $list = $db->where("id", ">", $this->startId)->orderBy("id", "asc")->get($this->mission);
        foreach ($list as $item) {
            $rows = ArchiveModel::emptyQuery()
                ->whereFindInSet($item["id"], "tags")
                ->rows();
            @TagsModel::emptyQuery()->where("id", $item["id"])->update(["archives" => $rows]);
            $this->debug("[{$item['id']}]: {$rows}");
        }
        $this->debug("统计结束");
    }

    private function debug(string $text)
    {
        Log::writeToEnd(Config::get("define.logs")."/cms/countTagsArchives_{$this->pid}.txt", $text);
    }
}